home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / TransSkel 3.24 / Demos / C Demos / Button / Modal2.c < prev    next >
Text File  |  1995-06-11  |  4KB  |  152 lines

  1. /*
  2.  * Modal Dialog 2
  3.  *
  4.  * This dialog consists of an edittext item for entering a search
  5.  * string, a statictext item for titling the edittext item, a Cancel
  6.  * button, a Find button, and a user item for outlining the default
  7.  * button.
  8.  * 
  9.  * The Cancel button is the default when the edittext item is empty.
  10.  * The Find button is the default when the edittext item contains text.
  11.  *
  12.  * This dialog demonstrates:
  13.  * - How to associate an outlining function with an arbitrary button.
  14.  * - How to change which button is the default, which involves moving
  15.  * the outlining function from one button to another.
  16.  */
  17.  
  18. # include    "TransSkel.h"
  19.  
  20. # include    "Button.h"
  21.  
  22.  
  23. typedef enum
  24. {
  25.     findItem = 1,
  26.     cancelItem,
  27.     staticTextItem,
  28.     editTextItem,
  29.     outlineItem
  30. };
  31.  
  32.  
  33. static short    defaultButton = 0;
  34.  
  35.  
  36. static pascal void OutlineButton (DialogPtr dlog, short item);
  37.  
  38.  
  39. /*
  40.  * Set up a variable to point to the drawing procedure.  For 68K code this
  41.  * is just a direct pointer to OutlineButton().  For PowerPC code it is a
  42.  * routine descriptor into which the address of OutlineButton() is stuffed.
  43.  */
  44.  
  45. # if skelPPC        /* PowerPC code */
  46.  
  47. static RoutineDescriptor    drawDesc =
  48.         BUILD_ROUTINE_DESCRIPTOR(uppUserItemProcInfo, OutlineButton);
  49. static UserItemUPP    drawProc = (UserItemUPP) &drawDesc;
  50.  
  51. # else                /* 68K code */
  52.  
  53. static UserItemUPP    drawProc = OutlineButton;
  54.  
  55. # endif
  56.  
  57.  
  58. /*
  59.  * Draw heavy outline around default dialog button.  This function is
  60.  * associated with the user item when there is a default button, and
  61.  * is called by ModalDialog() when the outline needs redrawing.
  62.  */
  63.  
  64. static pascal void
  65. OutlineButton (DialogPtr dlog, short item)
  66. {
  67.     SkelDrawButtonOutline (SkelGetDlogCtl (dlog, defaultButton));
  68. }
  69.  
  70.  
  71. /*
  72.  * Set up button given by item number as default button.
  73.  * Install an outlining procedure to associate outline user item with
  74.  * the given button and draw the outline item immediately.
  75.  */
  76.  
  77. static void
  78. SetDefaultButton (DialogPtr dlog, short item)
  79. {
  80. Rect    r;
  81.  
  82.     defaultButton = item;
  83.     SkelGetDlogRect (dlog, defaultButton, &r);    /* get button rect */
  84.     InsetRect (&r, -4, -4);                        /* expand it */
  85.     SkelSetDlogRect (dlog, outlineItem, &r);    /* use for outline item */
  86.     SkelSetDlogProc (dlog, outlineItem, drawProc);
  87.     SkelDrawButtonOutline (SkelGetDlogCtl (dlog, defaultButton));
  88. }
  89.  
  90.  
  91. void
  92. DoModal2 (void)
  93. {
  94. ModalFilterUPP    filter;
  95. DialogPtr    dlog;
  96. GrafPtr    savePort;
  97. short    item;
  98. short    newDefault;
  99. Str255    str;
  100.  
  101.     dlog = GetNewDialog (modal2Res, nil, (WindowPtr) -1L);
  102.     if (dlog == (DialogPtr) nil)
  103.     {
  104.         SysBeep (1);
  105.         return;
  106.     }
  107.  
  108.     SkelPositionWindow (dlog, skelPositionOnMainDevice, horizRatio, vertRatio);
  109.  
  110.     GetPort (&savePort);
  111.     SetPort (dlog);
  112.  
  113.     SetDefaultButton (dlog, cancelItem);
  114.  
  115.     ShowWindow (dlog);
  116.  
  117.     for (;;)
  118.     {
  119.         /*
  120.          * Get the standard filter and specify the default button so it will
  121.          * map return/enter.  Map escape and command-period onto the cancel
  122.          * button.
  123.          */
  124.         filter = SkelDlogFilter (nil, false);
  125.         SkelDlogDefaultItem (defaultButton);
  126.         SkelDlogCancelItem (cancelItem);
  127.         SkelDlogTracksCursor (true);
  128.         ModalDialog (filter, &item);
  129.         SkelRmveDlogFilter ();
  130.  
  131.         if (item == findItem || item == cancelItem)
  132.             break;
  133.  
  134.         /*
  135.          * Select Find button as default if editText item is
  136.          * non-empty.  Select Cancel button otherwise.  But
  137.          * don't actually do anything if the default button's
  138.          * already set correctly.
  139.          */
  140.  
  141.         SkelGetDlogStr (dlog, editTextItem, str);
  142.         newDefault = (str[0] > 0 ? findItem : cancelItem);
  143.         if (newDefault != defaultButton)
  144.         {
  145.             SkelEraseButtonOutline (SkelGetDlogCtl (dlog, defaultButton));
  146.             SetDefaultButton (dlog, newDefault);
  147.         }
  148.     }
  149.     DisposeDialog (dlog);
  150.     SetPort (savePort);
  151. }
  152.